package com.feonix.setup.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;

import com.feonix.common.BaseInfo;
import com.feonix.common.interceptor.BaseInterceptor;
import com.feonix.common.util.Properties;
import com.feonix.common.util.StringUtil;

public class LoginCheckInterceptor extends BaseInterceptor {

	/**
	 *在之前做
	 */
	@Override
	public boolean before(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object object) {
		HttpSession session = httpServletRequest.getSession();
		String userName = StringUtil.getString(session.getAttribute("setupuser"));
		String setupProp = LoginCheckInterceptor.class.getClassLoader().getResource("pub/config/").getPath() + "setup.properties";
		Properties properties = new Properties(setupProp);
		if(StringUtil.isEmpty(userName) || !StringUtil.equals(userName, properties.getString("adminName"))){
			try {
				httpServletResponse.sendRedirect(BaseInfo.getContextPath() + "/setup/login.do");
			} catch (IOException e) {
				logger.error("sendRedirect error:", e);
			}
			return false;
		}
		
		return true;
	}

	@Override
	public void after(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object,
			ModelAndView modelAndView) {
		// TODO Auto-generated method stub

	}

	@Override
	public void complete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object,
			Exception exception) {
		// TODO Auto-generated method stub

	}

}
